//
//  Regular.m
//  YiXiProject
//
//  Created by iMac on 2019/7/16.
//  Copyright © 2019 iMac. All rights reserved.
//

#import "Regular.h"

#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>

@implementation Regular

/**
 判断是否为手机号
 
 @param text 手机号
 @return 返回YES或NO 判断是否为手机号
 */
+(bool)isVerificationMobilePhone:(NSString *)text
{
    /**
     * 手机号码
     * 移动：134 135 136 137 138 139 147 150 151 152 157 158 159 178 182 183 184 187 188 198
     * 联通：130 131 132 145 155 156 166 171 175 176 185 186
     * 电信：133 149 153 173 177 180 181 189 199
     * 虚拟运营商: 170
     */
    //NSString *target = @"^(0|86|17951)?(13[0-9]|15[012356789]|16[6]|19[89]|17[01345678]|18[0-9]|14[579])[0-9]{8}$";
    NSString *target = @"^(0|86|17951)?(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])[0-9]{8}$";
    NSPredicate *targetPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", target];
    if ([targetPredicate evaluateWithObject:text]) {
        return YES;
    }
    return NO;
}

/**
 判断是否为邮箱号
 
 @param email 邮箱号
 @return 返回YES或NO 判断是否为邮箱号
 */
+(bool)isVerificationEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

/**
 判断用户密码是否6-18位数字和字母组合
 
 @param password 密码
 @return 返回YES或NO 判断是否符合密码格式
 */
+(bool)isVerificationPassword:(NSString *)password
{
    NSString *pattern =@"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{5,20}";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
    
    return [pred evaluateWithObject:password];
}

/**
 用户姓名,20位的中文或英文
 
 @param name 用户名
 @return 返回YES或NO 判断是否符合密码格式
 */
+(BOOL)isVerificationUserName:(NSString *)name;
{
    NSString *pattern =@"^[a-zA-Z\u4E00-\u9FA5]{1,20}";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
    
    return [pred evaluateWithObject:name];
}


/**
 *  验证有没有摄像头权限
 */
+(BOOL)isVerificationAVCaptureAuthorization
{
    
    NSString *mediaType = AVMediaTypeVideo;
    
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
        
        //NSLog(@"相机权限受限");
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请在iPhone的“设置-隐私-相机”选项中，允许当前app访问你的相机。" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:sure];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
        
        return NO;
    }
    
    return YES;
}

@end
